#ifndef CLOSURE_H
#define CLOSURE_H


struct Closure {
        virtual void operator() () = 0;
};



template <typename T>
Closure *new_closure( T *  lambda ) {

        struct LocalClosure : public Closure  {
                T * lambdap;

                LocalClosure( T *u ) {
                        lambdap = u;
                }


                void operator()() {

                        (*lambdap)();
                        // Commit suicide :
                        delete this;
                }

                virtual ~LocalClosure() {
                        delete lambdap;
                }
        };

        return new LocalClosure( lambda );

};

#define closure(z) new_closure( new auto( [=]() z ) )


#endif // CLOSURE_H
